package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.OrderDeleteBo;
import com.cskaoyan.bean.BO.OrderRefundBo;
import com.cskaoyan.bean.BO.OrderShipBo;
import com.cskaoyan.bean.PO.MarketOrder;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;


/**
 * @ClassName AdminOrderController
 * @Description TODO
 * @Author ke
 * @Date 2022/8/12 17:24
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    AdminOrderService orderService;


    @RequiresPermissions("admin:order:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, Short[] orderStatusArray, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date start, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date end, String orderSn,Integer userId){

        CommonData<MarketOrder> query = orderService.query(info, orderStatusArray, start, end, orderSn,userId);
        BaseRespVo<CommonData> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setData(query);
        baseRespVo.setErrmsg("成功");

        return baseRespVo;
    }

    @RequiresPermissions("admin:order:delete")
    @RequestMapping("delete")
    public BaseRespVo deleteNLog(@RequestBody OrderDeleteBo bo){

        Integer code = orderService.delete(bo);

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 2);
        session.setAttribute("logAction", "删除订单");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult","");
        session.setAttribute("logComment","");
        session.setAttribute("logOrderId",bo.getOrderId());

        if (code == -1) {

            session.setAttribute("logStatus",false);
            session.setAttribute("logResult","订单不能删除");

            return BaseRespVo.ok(632,null,"订单不能删除");

        }




        return BaseRespVo.ok(0,null,"成功");
    }


    @RequiresPermissions("admin:order:read")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
      BaseRespVo baseRespVo=orderService.detail(id);
        return  baseRespVo;
    }

    @RequiresPermissions("admin:order:ship")
    @RequestMapping("ship")
    public BaseRespVo shipNLog(@RequestBody OrderShipBo bo){
        BaseRespVo baseRespVo=  orderService.ship(bo);

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 2);
        session.setAttribute("logAction", "发货");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult","");
        session.setAttribute("logComment","");
        session.setAttribute("logOrderId",bo.getOrderId());

        return baseRespVo;
    }


    @RequestMapping("channel")
    public BaseRespVo channel(){
        BaseRespVo baseRespVo = orderService.channel();
        return baseRespVo;
    }

    @RequiresPermissions("admin:order:refund")
    @RequestMapping("refund")
    public BaseRespVo refundNLog(@RequestBody OrderRefundBo bo){
        orderService.refund(bo);

        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("logType", 2);
        session.setAttribute("logAction", "退款");
        session.setAttribute("logStatus",true);
        session.setAttribute("logResult","");
        session.setAttribute("logComment","");
        session.setAttribute("logOrderId",bo.getOrderId());

        return BaseRespVo.ok(0,null,"成功");
    }
}
